package learnonline.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import learnonline.dao.RowMapper;
import learnonline.dao.StudentDao;
import learnonline.entity.Student;


public class StudentDaoImpl implements StudentDao{
	JdbcImpl jdbc =new JdbcImpl();
	private static class StudentRowMapper implements RowMapper<Student>{

		@Override
		public Student mapRow(ResultSet rs) throws SQLException {
			Student s=new Student();
			s.setSid(rs.getInt("sid"));
			s.setSname(rs.getString("sname"));
			s.setEmail(rs.getString("email"));
			s.setBalance(rs.getDouble("balance"));
			return s;
		}
		
	}
	@Override
	public List<Student> getStudentList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Student fetchStudentBy(String sname) {
		String sql ="select * from student where sname = ?";
		List<Student> list =jdbc.queryList(sql, new StudentRowMapper(), sname);
		return list.size()>0?list.get(0):null;
	}

	@Override
	public boolean addStudent(int sid, String sname, String email, double balance) {
		ArrayList parameters = new ArrayList();
		parameters.add(sid);
		parameters.add(sname);
		parameters.add(email);
		parameters.add(balance);
		String sql = "insert into student(sid,sname,email,balance)values(?,?,?,?)";
		return jdbc.executeUpdate(sql, parameters.toArray());
	}
}
